﻿using AutoMapper;
using IMES.Common.Domain.KeepModel.Process;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;
using NPOI.SS.Formula.Functions;

namespace Keep.API.Appliction.Command.ProcessFile
{
    /// <summary>
    /// 工艺路线列表
    /// </summary>
    public class ProcessRouteListHandler:IRequestHandler<ProcessRouteListCommon,Result>
    {
        private readonly IBaseRepositorys<ProcessRoute> _processRouteRep; 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="processRouteRep"></param>
       public ProcessRouteListHandler(IBaseRepositorys<ProcessRoute> processRouteRep)
        {
            _processRouteRep = processRouteRep; 
        }
        /// <summary>
        /// 处理器
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public  Task<Result> Handle(ProcessRouteListCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.success, Messege = "查询成功" };
            try
            {
                var list = _processRouteRep.GetAll();
                if(request.ProcessRouteNumber!=null)
                {
                    list = list.Where(a => a.ProcessRouteNumber == request.ProcessRouteNumber);
                }
                if(list==null)
                {
                    result.Code = (int)CodeEnum.search;
                    result.Messege = "暂无数据";
                    return Task.FromResult(result);
                } 
                    result.Data = list; 
            }
            catch (Exception ex)
            {
                result.Code = (int)CodeEnum.error;
                result.Messege = ex.Message;
            }
            return Task.FromResult(result); 
        }


    }
}
